package com.scalar.chiptrack.operations.dataentry;

import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.operations.process.dao.PartNumber;
import com.scalar.chiptrack.operations.process.VendorLocation;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;
import com.scalar.chiptrack.operations.dataentry.dao.DieBankBinningInfoDAO;
import com.scalar.chiptrack.operations.dataentry.forms.DieBankBinningInfoForm;
import com.scalar.chiptrack.commons.Constants;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.ScalarException;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * User: Murali Krishna Yengala
 * Date: Jul 22, 2005
 * Time: 5:27:24 PM
 */
public class DataEntryHelper implements Constants{

    public static void loadSortPartNumbers(HttpServletRequest request) throws ScalarException
    {
        ArrayList partNumbersList = ProductDao.getPartNumber ( "SORT" );
        PartNumber partNumber = null;

        ArrayList partNumberSelectList = new ArrayList ();

        for ( int i = 0; i < partNumbersList.size (); i++ )
        {
            partNumber = ( PartNumber ) partNumbersList.get ( i );
            partNumberSelectList.add ( new org.apache.struts.util.LabelValueBean ( partNumber.getPartNumber (), partNumber.getProcessStepId () ) );
        }
        if( partNumberSelectList != null && partNumberSelectList.size() > 0)
        {
            request.setAttribute( "partNumbers", partNumberSelectList);
        }
        else
        {
            HashMap info = (HashMap) request.getAttribute( SessionKeys.INFO_MESSAGES );
            info.put("No Sort Parts Found", null);
        }
    }


    public static void loadLocations( int processStepId, HttpServletRequest request) throws Exception
    {
        ArrayList locations = VendorDAO.getSelectedLocations ( processStepId );
        ArrayList vendorLocationList = new ArrayList();
        for ( int i = 0; i < locations.size (); i++ )
        {
            VendorLocation vendorLocation = ( VendorLocation ) locations.get ( i );
            vendorLocation.setLocationID ( vendorLocation.getLocationID () );
            vendorLocation.setLocationName ( vendorLocation.getLocationName () );
            vendorLocationList.add ( new org.apache.struts.util.LabelValueBean ( vendorLocation.getLocationName (), vendorLocation.getLocationID () ) );
        }
        if( vendorLocationList != null && vendorLocationList.size() > 0)
        {
            request.setAttribute("vendorLocations", vendorLocationList);
        }
        else
        {
            HashMap info = (HashMap) request.getAttribute( SessionKeys.INFO_MESSAGES );
            info.put("No Locations for the Sort Part could be Found", null);
        }
    }

    public static void loadSortLots(int processStepId, int locationId, HttpServletRequest request) throws ScalarException
    {
        ArrayList lots = DieBankBinningInfoDAO.getSortLots( processStepId, locationId);
        if( lots != null && lots.size() > 0 )
        {
            request.setAttribute( "sortLots" ,lots);
        }
        else
        {
            HashMap info = (HashMap) request.getAttribute( SessionKeys.INFO_MESSAGES );
            info.put("No Lots could be found for the Part Number and Location", null);
        }
    }

    public static void resetForm ( DieBankBinningInfoForm dieBankBinningInfoForm )
    {
        dieBankBinningInfoForm.setProcessStepId("0");
        dieBankBinningInfoForm.setLotId("0");
        dieBankBinningInfoForm.setLocationId("0");
        dieBankBinningInfoForm.setWafers(null);
        dieBankBinningInfoForm.setGrossDie(0);
    }
}
